package com.mes.service;

import com.mes.model.Material;
import com.mes.repository.MaterialRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialService {

    @Autowired
    private MaterialRepository materialRepository;

    public List<Material> getAllMaterials() {
        return materialRepository.findAll();
    }

    public Material createMaterial(Material material) {
        return materialRepository.save(material);
    }

    public Material updateMaterialQuantity(Long id, Integer quantity) {
        Material material = materialRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Material with id " + id + " not found"));
        material.setQuantity(quantity);
        return materialRepository.save(material);
    }
}
